home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / vector / ByteVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  10KB  |  282 lines

  1. #ifndef    BYTEVEC_H
  2. #define    BYTEVEC_H
  3.  
  4. /* ByteVec.h -- Unsigned Character Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.         Ted Persky
  15.     Bg. 12A, Rm. 2031
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-2963
  21.     uucp: uunet!nih-csl!tpersky
  22.     Internet:tpersky@alw.nih.gov
  23.  
  24. Modification History:
  25.  
  26. $Log:    ByteVec.h,v $
  27.  * Revision 3.0  90/05/16  23:00:27  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31. #include "Vector.h"
  32. #include "BitVec.h"
  33. #include "IntVec.h"
  34.  
  35. typedef unsigned char bytevecbyte;
  36.  
  37. class ByteSlice;
  38. class BytePick;
  39. class ByteSlct;
  40.  
  41. class ByteVec : public Vector {
  42.     DECLARE_MEMBERS(ByteVec);
  43.     bytevecbyte* v;        // pointer to data, NULL if empty vector
  44.     void indexRangeErr() const;
  45. protected:
  46.     virtual void storer(OIOofd&) const;
  47.     virtual void storer(OIOout&) const;
  48. public:
  49.     ByteVec(unsigned len =0);
  50.     ByteVec(unsigned len, bytevecbyte from, bytevecbyte by =1);
  51.     ByteVec(const bytevecbyte*, unsigned len);
  52.     ByteVec(const ByteVec&);
  53.     ByteVec(const ByteSlice&);
  54.     ~ByteVec()            { delete v; }
  55.     ByteSlice operator()(int pos, unsigned lgt, int stride =1);
  56.     const ByteSlice operator()(int pos, unsigned lgt, int stride =1) const;
  57.     bytevecbyte* pt()        { return v; }
  58.     const bytevecbyte* pt() const    { return v; }
  59.     operator ByteSlice();
  60.     operator const ByteSlice() const;
  61.     operator DoubleVec();
  62. //    operator LongVec();
  63.     bytevecbyte& operator[](int i) {    // vector element
  64.         if ((unsigned)i >= n) indexRangeErr();
  65.         return v[i];
  66.     }
  67.     const bytevecbyte& operator[](int i) const {    // vector element
  68.         if ((unsigned)i >= n) indexRangeErr();
  69.         return v[i];
  70.     }
  71.     bytevecbyte& operator()(int i)            { return v[i]; }
  72.     const bytevecbyte& operator()(int i) const  { return v[i]; }
  73.     BytePick operator[](const IntVec&);
  74.     const BytePick operator[](const IntVec&) const;
  75.     ByteSlct operator[](const BitVec&);
  76.     const ByteSlct operator[](const BitVec&) const;
  77.     void /*ByteVec::*/operator=(const ByteVec&);
  78.     void /*ByteVec::*/operator=(const ByteSlice&);
  79.     void /*ByteVec::*/operator=(const ByteSlct&);
  80.     void /*ByteVec::*/operator=(const BytePick&);
  81.     void /*ByteVec::*/operator=(bytevecbyte);
  82.     void /*ByteVec::*/lengthErr(const ByteSlice&) const;
  83.     void selectErr(const BitVec&) const;
  84.     virtual void deepenShallowCopy();
  85.     virtual unsigned hash() const;
  86.     virtual bool isEqual(const Object&) const;
  87.     virtual void printOn(ostream& strm =cout) const;
  88.     virtual void scanFrom(istream& strm);
  89.     virtual void sort();
  90.     virtual const Class* species() const;
  91. };
  92.  
  93. class TempByteVec : public ByteVec {
  94.     friend ByteSlice;
  95.     friend BytePick;
  96.     friend ByteSlct;
  97.     TempByteVec(unsigned len =0) : ByteVec(len) {}
  98.     virtual void free();
  99. };
  100.  
  101. class ByteSlice : public NIHCL {
  102.     ByteVec* V;    // vector pointer
  103.     bytevecbyte* p;    // slice pointer
  104.     unsigned l;    // slice length
  105.     int k;    // slice stride
  106.     ByteSlice(const ByteVec& v, int pos, unsigned lgt, int stride =1);
  107.     ByteSlice(const ByteVec& v, unsigned lgt) {
  108.         V = &(ByteVec&)v;  p = ((ByteVec&)v).pt();  l = lgt;  k = 1;
  109.     }
  110.     ByteSlice(const ByteSlice&);
  111.     friend ByteVec;
  112. public:
  113.     ByteSlice(const BytePick&);
  114.     ByteSlice(const ByteSlct&);
  115.     ~ByteSlice()        { V->free(); }
  116.     bytevecbyte* pt()        { return p; }
  117.     const bytevecbyte* pt() const    { return p; }
  118.     unsigned length() const    { return l; }
  119.     int stride() const    { return k; }
  120.     void /*ByteSlice::*/operator=(const ByteVec&);
  121.     void /*ByteSlice::*/operator=(const BytePick&);
  122.     void /*ByteSlice::*/operator=(const ByteSlct&);
  123.     void /*ByteSlice::*/operator=(const ByteSlice&);
  124.     void /*ByteSlice::*/operator=(bytevecbyte);
  125.     void /*ByteSlice::*/lengthErr(const ByteVec&) const;
  126.     void /*ByteSlice::*/lengthErr(const ByteSlice&) const;
  127.     void /*ByteSlice::*/lengthErr(const IntVec&) const;
  128.     void selectErr(const BitVec&) const;
  129. friend    ByteVec    operator!(const ByteSlice&);
  130. friend    ByteVec    operator~(const ByteSlice&);
  131. friend    ByteVec    operator++(ByteSlice&);
  132. friend    ByteVec    operator--(ByteSlice&);
  133. friend    ByteVec    operator*(const ByteSlice&,const ByteSlice&);
  134. friend    ByteVec    operator/(const ByteSlice&,const ByteSlice&);
  135. friend    ByteVec    operator%(const ByteSlice&,const ByteSlice&);
  136. friend    ByteVec    operator+(const ByteSlice&,const ByteSlice&);
  137. friend    ByteVec    operator-(const ByteSlice&,const ByteSlice&);
  138. friend    ByteVec    operator&(const ByteSlice&,const ByteSlice&);
  139. friend    ByteVec    operator^(const ByteSlice&,const ByteSlice&);
  140. friend    ByteVec    operator|(const ByteSlice&,const ByteSlice&);
  141. friend    ByteVec    operator*(const ByteSlice&,bytevecbyte);
  142. friend    ByteVec    operator*(bytevecbyte s,const ByteSlice& V)  { return V*s; }
  143. friend    ByteVec    operator/(const ByteSlice&,bytevecbyte);
  144. friend    ByteVec    operator/(bytevecbyte,const ByteSlice&);
  145. friend    ByteVec    operator%(const ByteSlice&,bytevecbyte);
  146. friend    ByteVec    operator%(bytevecbyte,const ByteSlice&);
  147. friend    ByteVec    operator+(const ByteSlice&,bytevecbyte);
  148. friend    ByteVec    operator+(bytevecbyte s,const ByteSlice& V)  { return V+s; }
  149. friend    ByteVec    operator-(const ByteSlice&,bytevecbyte);
  150. friend    ByteVec    operator-(bytevecbyte,const ByteSlice&);
  151. friend    ByteVec    operator&(const ByteSlice&,bytevecbyte);
  152. friend    ByteVec    operator&(bytevecbyte s,const ByteSlice& V)  { return V&s; }
  153. friend    ByteVec    operator^(const ByteSlice&,bytevecbyte);
  154. friend    ByteVec    operator^(bytevecbyte s,const ByteSlice& V)  { return V^s; }
  155. friend    ByteVec    operator|(const ByteSlice&,bytevecbyte);
  156. friend    ByteVec    operator|(bytevecbyte s,const ByteSlice& V)  { return V|s; }
  157. friend    BitVec    operator<(const ByteSlice&,const ByteSlice&);
  158. friend    BitVec    operator>(const ByteSlice& U,const ByteSlice& V)    { return V < U; }
  159. friend    BitVec    operator<=(const ByteSlice&,const ByteSlice&);
  160. friend    BitVec    operator>=(const ByteSlice& U,const ByteSlice& V) { return V <= U; }
  161. friend    BitVec    operator==(const ByteSlice&,const ByteSlice&);
  162. friend    BitVec    operator!=(const ByteSlice&,const ByteSlice& V);
  163. friend    BitVec    operator<(const ByteSlice&,bytevecbyte);
  164. friend    BitVec    operator<(bytevecbyte s,const ByteSlice& V)  { return V > s; }
  165. friend    BitVec    operator>(const ByteSlice&,bytevecbyte);
  166. friend    BitVec    operator>(bytevecbyte s,const ByteSlice& V)  { return V < s; }
  167. friend    BitVec    operator<=(const ByteSlice&,bytevecbyte);
  168. friend    BitVec    operator<=(bytevecbyte s,const ByteSlice& V) { return V >= s; }
  169. friend    BitVec    operator>=(const ByteSlice&,bytevecbyte);
  170. friend    BitVec    operator>=(bytevecbyte s,const ByteSlice& V) { return V <= s; }
  171. friend    BitVec    operator==(const ByteSlice&,bytevecbyte);
  172. friend    BitVec    operator==(bytevecbyte s,const ByteSlice& V) { return V == s; }
  173. friend    BitVec    operator!=(const ByteSlice&,bytevecbyte);
  174. friend    BitVec    operator!=(bytevecbyte s,const ByteSlice& V) { return V != s; }
  175. friend    void    operator+=(ByteSlice&,const ByteSlice&);
  176. friend    void    operator+=(ByteSlice&,bytevecbyte);
  177. friend    void    operator-=(ByteSlice&,const ByteSlice&);
  178. friend    void    operator-=(ByteSlice&,bytevecbyte);
  179. friend    void    operator*=(ByteSlice&,const ByteSlice&);
  180. friend    void    operator*=(ByteSlice&,bytevecbyte);
  181. friend    void    operator/=(ByteSlice&,const ByteSlice&);
  182. friend    void    operator/=(ByteSlice&,bytevecbyte);
  183. friend    void    operator%=(ByteSlice&,const ByteSlice&);
  184. friend    void    operator%=(ByteSlice&,bytevecbyte);
  185. friend    void    operator&=(ByteSlice&,const ByteSlice&);
  186. friend    void    operator&=(ByteSlice&,bytevecbyte);
  187. friend    void    operator^=(ByteSlice&,const ByteSlice&);
  188. friend    void    operator^=(ByteSlice&,bytevecbyte);
  189. friend    void    operator|=(ByteSlice&,const ByteSlice&);
  190. friend    void    operator|=(ByteSlice&,bytevecbyte);
  191. friend    ByteVec    cumsum(const ByteSlice&);
  192. friend    ByteVec    delta(const ByteSlice&);
  193. friend    bytevecbyte    dot(const ByteSlice&,const ByteSlice&);
  194. friend    int    max(const ByteSlice&);
  195. friend    int    min(const ByteSlice&);
  196. friend    bytevecbyte    prod(const ByteSlice&);
  197. friend    ByteVec    reverse(const ByteSlice&);
  198. friend    bytevecbyte    sum(const ByteSlice&);
  199. };
  200.  
  201. class BytePick : public NIHCL {
  202.     ByteVec* V;
  203.     const IntVec* X;
  204.     BytePick(const ByteVec& v,const IntVec& x)    { V = &(ByteVec&)v;  X = &x; }
  205.     BytePick(const BytePick& s)            { V = s.V; X = s.X; }
  206.     friend ByteVec;
  207.     friend ByteSlice;
  208.     friend ByteSlct;
  209. public:
  210.     void /*BytePick::*/operator=(const ByteVec&);
  211.     void /*BytePick::*/operator=(const BytePick&);
  212.     void /*BytePick::*/operator=(const ByteSlct&);
  213.     void /*BytePick::*/operator=(const ByteSlice&);
  214.     void /*BytePick::*/operator=(bytevecbyte);
  215.     unsigned length() const    { return X->length(); }
  216. };
  217.  
  218. class ByteSlct : public NIHCL {
  219.     ByteVec* V;
  220.     const BitVec* B;
  221.     ByteSlct(const ByteVec& v, const BitVec& b)    { V = &(ByteVec&)v;  B = &b; }
  222.     ByteSlct(const ByteSlct& s)            { V = s.V; B = s.B; }
  223.     friend ByteVec;
  224.     friend ByteSlice;
  225.     friend BytePick;
  226. public:
  227.     void /*ByteSlct::*/operator=(const ByteVec&);
  228.     void /*ByteSlct::*/operator=(const BytePick&);
  229.     void /*ByteSlct::*/operator=(const ByteSlct&);
  230.     void /*ByteSlct::*/operator=(const ByteSlice&);
  231.     void /*ByteSlct::*/operator=(bytevecbyte);
  232.     unsigned length() const    { return B->length(); }
  233. };
  234.  
  235. inline ByteSlice ByteVec::operator()(int pos, unsigned lgt, int stride)
  236. {
  237.     ByteSlice s(*this,pos,lgt,stride);
  238.     return s;
  239. }
  240.  
  241. inline const ByteSlice ByteVec::operator()(int pos, unsigned lgt, int stride) const
  242. {
  243.     const ByteSlice s(*this,pos,lgt,stride);
  244.     return s;
  245. }
  246.  
  247. inline ByteVec::operator ByteSlice()
  248. {
  249.     ByteSlice s(*this,length());
  250.     return s;
  251. }
  252.  
  253. inline ByteVec::operator const ByteSlice() const
  254. {
  255.     const ByteSlice s(*this,length());
  256.     return s;
  257. }
  258.  
  259. inline BytePick ByteVec::operator[](const IntVec& I)
  260. {
  261.     return BytePick(*this,I);
  262. }
  263.  
  264. inline const BytePick ByteVec::operator[](const IntVec& I) const
  265. {
  266.     const BytePick t(*this,I);
  267.     return t;
  268. }
  269.  
  270. inline ByteSlct ByteVec::operator[](const BitVec& B)
  271. {
  272.     return ByteSlct(*this,B);
  273. }
  274.  
  275. inline const ByteSlct ByteVec::operator[](const BitVec& B) const
  276. {
  277.     const ByteSlct t(*this,B);
  278.     return t;
  279. }
  280.  
  281. #endif
  282.